﻿@using iTextSharp.text;
@using iTextSharp.text.pdf;
@using iTextSharp.text.html;
@using iTextSharp.text.xml;
@using System.Collections.Generic;
@using System.Collections;
@using System.Text;

@{
    Layout = "~/_SiteLayout.cshtml";

    var custid = UtilityManager.GetVal(Request["custId"]);
     var ccno = UtilityManager.GetVal(Request["ccno"]);
     var fc = UtilityManager.GetVal(Request["fieldChanged"]);
     var changeBy = UtilityManager.GetVal(Request["changedBy"]);
     var toc = UtilityManager.GetVal(Request["toc"]);
     var ov = UtilityManager.GetVal(Request["oldValue"]);
     var nv = UtilityManager.GetVal(Request["newValue"]);
     var dateFrom = UtilityManager.GetDateVal(Request["dateFrom"]);
     var dateTo = UtilityManager.GetDateVal(Request["dateTo"]);
   
    
    
    //var query = "SELECT * FROM AuditTrail WHERE DateCreated >= @0 AND DateCreated <= @1";

     var query = "SELECT * FROM AuditTrail ";
    StringBuilder sb = new StringBuilder();
    sb.Append(query);
    
    //check params
    int pCtr=0;
    Dictionary<string,dynamic> p = new Dictionary<string,dynamic>();
    if(custid!=""){
        p.Add("CustomerID",custid);
        pCtr++;
    }
    if(ccno!=""){
        p.Add("CashCardNo",ccno);
        pCtr++;
    }
    if(fc!=""){
        p.Add("FieldChange",fc);
        pCtr++;
    }
    if(changeBy!=""){
        p.Add("ChangeBy",changeBy);
        pCtr++;
    }
    if(toc!=""){
        p.Add("TypeOfChange",toc);
        pCtr++;
    }
    if(ov!=""){
        p.Add("OldValue",ov);
        pCtr++;
    }
    if(nv!=""){
        p.Add("NewValue",nv);
        pCtr++;
    }
    
    if(dateFrom!=null){
        p.Add("DateFrom",dateFrom);
        pCtr++;
    }
    
   /* if(dateTo!=null){
        p.Add("DateTo",dateTo);
        pCtr++;
    }*/
    
    if(pCtr>0){
        int ctr=0;
        string cond=string.Empty;
        foreach(KeyValuePair<string,dynamic> pair in p){
            if(ctr==0){
                cond =" WHERE";
            }else{
                cond=" AND";
            }
            if((string.Compare(pair.Key,"FieldChange")==0) || (string.Compare(pair.Key,"ChangeBy")==0) || (string.Compare(pair.Key,"TypeOfChange")==0)|| (string.Compare(pair.Key,"OldValue")==0) || (string.Compare(pair.Key,"NewValue")==0)){
                     sb.Append(cond).Append(string.Format(" {0} LIKE '{1}%'",pair.Key,pair.Value));
            }else if(string.Compare(pair.Key,"DateFrom")==0){
                  var newDF = Convert.ToDateTime(dateFrom);
                 var newDT = Convert.ToDateTime(dateTo);
              
                 sb.Append(cond).Append(string.Format(" DateCreated>='{0}' AND DateCreated<='{1}'",newDF,newDT.AddDays(1)));
            }else{
                sb.Append(cond).Append(string.Format(" {0}='{1}'",pair.Key,pair.Value));
            }
             ctr++;   
        }
    }
    
    
    var cols = new[]{"FieldChange","TypeOfChange","OldValue","NewValue","ChangeBy"};
    
    
    var reportData = DBManager.Query(sb.ToString(),null);
   

    DateTime today = DateTime.Today;
    //string ctr = UtilityManager.GetCounterForFileExt();
    string fileName=string.Empty;

   
    var doc = new Document(PageSize.LEGAL,10,10,10,10);
    var pdf = Server.MapPath("~/Content/Templates/ReportTemplate.pdf");
    PdfPTable table = null;
   
  
    fileName = string.Format("AuditReport_{0}.pdf",today);
    table = ReportManager.GeneratePDF(6,reportData);
    doc.SetPageSize(iTextSharp.text.PageSize.LEGAL.Rotate());
      
    
    
    PdfWriter.GetInstance(doc, new FileStream(pdf, FileMode.Create));
   
  
    doc.Open();
    doc.Add(table);
    doc.Close();
    
    Response.AddHeader("Content-Type", "application/pdf");
    Response.AddHeader("content-disposition", "attachment;filename="+fileName);
    Response.TransmitFile(pdf);
}
